package com.compass.doctor.ui.mine

import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.compass.doctor.base.BaseVBActivity
import com.compass.doctor.callback.OnCustomButtonClick
import com.compass.doctor.databinding.ActivityBankCardBinding
import com.compass.doctor.databinding.LayoutAddBankcardBinding
import com.compass.doctor.ui.mine.adapter.BankCardAdapter
import com.compass.doctor.ui.mine.viewmodel.IntegralViewModel
import com.compass.framework.R
import com.compass.framework.constant.KEY_CONSULT_TYPE
import com.compass.framework.decoration.NormalItemDecoration
import com.compass.framework.dialog.CommonMessageDialog
import com.compass.framework.ext.onClick
import com.compass.framework.utils.StatusBarSettingHelper
import com.compass.framework.utils.dpToPx
import com.compass.framework.utils.getStringFromResource

/**
 * Description： 银行卡
 * Date:2023/12/26 21:07
 */
class BankCardActivity : BaseVBActivity<ActivityBankCardBinding>() {

    companion object {
        fun start(context: Context) {
            val intent = Intent(context, BankCardActivity::class.java)
            context.startActivity(intent)
        }
    }

    //添加银行卡回调
    private val mActivityResultLauncher: ActivityResultLauncher<Intent>? =
        registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
            if (it.resultCode == RESULT_OK) {
                integralViewModel.getBankCardList()
            }
        }
    private val integralViewModel: IntegralViewModel by viewModels()
    private lateinit var bankcardFloorBinding: LayoutAddBankcardBinding
    private lateinit var bankCardAdapter: BankCardAdapter
    override fun initView(savedInstanceState: Bundle?) {
        StatusBarSettingHelper.setStatusBarTranslucent(this)
        StatusBarSettingHelper.statusBarLightMode(this@BankCardActivity, true)
        bankcardFloorBinding = LayoutAddBankcardBinding.inflate(LayoutInflater.from(this))
        bankCardAdapter = BankCardAdapter()
        bankCardAdapter.addFootView(bankcardFloorBinding.root)
        mBinding.rcyCard.apply {
            layoutManager = LinearLayoutManager(this@BankCardActivity)
            addItemDecoration(NormalItemDecoration().apply {
                setBounds(
                    top = dpToPx(10),
                    left = dpToPx(10),
                    right = dpToPx(10),
                    bottom = dpToPx(10)
                )
            })
            adapter = bankCardAdapter
        }
        bankcardFloorBinding.llAddCard.onClick {
            val intent = Intent(this, AddBankCardActivity::class.java)
            intent.putExtra(KEY_CONSULT_TYPE, 1)
            mActivityResultLauncher?.launch(intent)
        }
        bankCardAdapter.setBottomCallBack(object : OnCustomButtonClick {

            override fun onButtonCallBack(position: Int, msgType: String?) {
                val item = bankCardAdapter.getItem(position)

                item?.let {
                    when (msgType) {
                        "untie" -> {
                            untieCard(item.id)
                        }

                        "cardNumber" -> {
                            integralViewModel.getCardNumber(item.id)
                        }
                    }

                }


            }
        })

    }

    override fun initData() {
        integralViewModel.getBankCardList()
        integralViewModel.cardListLiveData.observe(this) {
            bankCardAdapter.setData(it)
        }
        integralViewModel.deleteCardLiveData.observe(this) {
            if (it) {
                showToast("删除成功")
                integralViewModel.getBankCardList()
            }
        }
        integralViewModel.careNumberLiveData.observe(this) { cardNumber ->
            cardNumber?.let {
                seeCardNumber(cardNumber)
            }

        }
    }

    fun untieCard(cardId: String) {
        CommonMessageDialog.Builder(this)
            .setTitle(getStringFromResource(R.string.dialog_tips_title))
            .setMessage(getStringFromResource(com.compass.doctor.R.string.setting_untie_tips))
            .setConfirm(getStringFromResource(R.string.default_confirm))

            .setCancel(getString(com.compass.framework.R.string.default_cancel))
            .setonCancelListener {
                it?.dismiss()
            }
            .setonConfirmListener {
                integralViewModel.deleteBankCard(cardId)
                it?.dismiss()
            }.create().show()

    }


    private fun seeCardNumber(cardNumber: String) {
        CommonMessageDialog.Builder(this)
            .setTitle("完整卡号")
            .setMessage(cardNumber)
            .setConfirm("复制")

            .setCancel(getString(com.compass.framework.R.string.default_cancel))
            .setonCancelListener {
                it?.dismiss()
            }
            .setonConfirmListener {
                 copyText(cardNumber)
                it?.dismiss()
            }.create().show()

    }

    private fun copyText(text: String) {
        val clipboardManager = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
        val clipData = ClipData.newPlainText("text", text)
        clipboardManager.setPrimaryClip(clipData)
        showToast("复制成功")


    }
}